package com.example.physicalexaminationsystem.service;

import cn.hutool.json.JSONUtil;
import com.example.physicalexaminationsystem.constans.RedisKeys;
import com.example.physicalexaminationsystem.mapper.MealDao;
import com.example.physicalexaminationsystem.mapper.ProjectDao;
import com.example.physicalexaminationsystem.model.MealEntity;
import com.example.physicalexaminationsystem.model.ProjectEntity;
import com.example.physicalexaminationsystem.vo.MealVO;
import com.example.physicalexaminationsystem.vo.ProjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author shiwenzhuo
 * 
 * @description
 */
@Component
public class MealService {
    @Autowired
    private MealDao mealDao;
    @Autowired
    private ProjectDao projectDao;
    @Resource
    private ProjectService projectService;
    @Autowired
    private Jedis jedis;
    //添加套餐信息
    public void addMeal(Map<String, Object> params) {
        MealEntity mealEntity = new MealEntity();
        mealEntity.setName((String) params.get("name"));
        mealEntity.setCategory((String) params.get("category"));
        mealEntity.setPrice((Integer) params.get("price"));
        mealEntity.setInfo((String) params.get("info"));
        mealDao.insert(mealEntity);
        Object projectIds = params.get("projectIdList");
        List<Long> projectIdList = JSONUtil.parseArray(projectIds).toList(Long.class);
        params.remove("projectIdList");
        List<MealEntity> mealEntities = mealDao.selectByMap(params);
        Long id = mealEntities.get(0).getId();
        String idListKey = String.format(RedisKeys.MEAL_PROJECT_ID_LIST, id);
        for (Long aLong : projectIdList) {
            ProjectEntity projectEntity = projectDao.selectById(aLong);
            if (projectEntity == null) {
                continue;
            }
            jedis.zadd(idListKey, System.currentTimeMillis(), aLong.toString());
        }
    }

    //删除套餐信息根据id
    public void deleteMealById(Long id) {
        mealDao.deleteById(id);
    }

    //修改套餐信息
    public void updateMealByid(Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        String category = params.get("category").toString();
        String name = params.get("name").toString();
        Integer price = Integer.valueOf(params.get("price").toString());
        String info = params.get("info").toString();
        Object projectIds = params.get("projectIdList");
        List<Long> projectIdList = JSONUtil.parseArray(projectIds).toList(Long.class);
        String idListKey = String.format(RedisKeys.MEAL_PROJECT_ID_LIST, id);
        for (Long aLong : projectIdList) {
            jedis.zadd(idListKey, System.currentTimeMillis(), aLong.toString());
        }
        MealEntity mealEntity = new MealEntity();
        mealEntity.setId(id);
        mealEntity.setCategory(category);
        mealEntity.setName(name);
        mealEntity.setPrice(price);
        mealEntity.setInfo(info);
        mealDao.updateById(mealEntity);
    }

    //获取套餐类型枚举
    public List<String> getMealCategory() {
        List<String> result = new ArrayList<>();
        result.add("个人套餐");
        result.add("公司套餐");
        return result;
    }

    //获取所有套餐信息
    public List<MealVO> getAllMeal() {
        List<MealVO> result = new ArrayList<>();
        List<MealEntity> mealEntities = mealDao.selectList(null);
        for (MealEntity mealEntity : mealEntities) {
            MealVO allMealVO = getMealVOByMealEntity(mealEntity);
            result.add(allMealVO);
        }
        return result;
    }

    public MealVO getMealVOByMealEntity(MealEntity mealEntity) {
        MealVO allMealVO = new MealVO();
        allMealVO.setId(mealEntity.getId());
        allMealVO.setName(mealEntity.getName());
        allMealVO.setCategory(mealEntity.getCategory());
        allMealVO.setPrice(mealEntity.getPrice());
        allMealVO.setInfo(mealEntity.getInfo());
        allMealVO.setProjectList(new ArrayList<>());
        String idListKey = String.format(RedisKeys.MEAL_PROJECT_ID_LIST, mealEntity.getId());
        jedis.zrange(idListKey, 0, -1).forEach(id -> {
            ProjectEntity projectEntity = getProjectById(Integer.parseInt(id));
            ProjectVO projectVO = projectService.ProjectEntityToProjectVO(projectEntity);
            allMealVO.getProjectList().add(projectVO);
        });
        return allMealVO;
    }

    public ProjectEntity getProjectById(Integer id) {
        return projectDao.selectById(id);
    }
}
